package com.tjetc.view.studentview;

import com.tjetc.proxy.CilentProxy;
import com.zhiyun.domain.Course;
import com.zhiyun.domain.CourseContent;
import com.zhiyun.domain.CourseUnit;
import com.zhiyun.service.CourseContentService;
import com.zhiyun.service.CourseUnitService;
import com.zhiyun.vo.CourseFile;
import com.zhiyun.vo.Result;

import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Arrays;
import java.util.List;
import java.util.Scanner;

public class MyCourseView_In_Content {
    CourseUnitService courseUnitService=CilentProxy.getProxyInstance(CourseUnitService.class);
    CourseContentService courseContentService=CilentProxy.getProxyInstance(CourseContentService.class);
    Scanner scanner=new Scanner(System.in);
    public void start(Course course){
        while (true){
            System.out.println("~~~~~~~~~~~课程内容~~~~~~~~~~");
            System.out.println("1.课程内容目录");
            System.out.println("2.查看单个单元内容");
            System.out.println("3.下载单元内容");
            System.out.println("0. 返回上一级");
            int i = scanner.nextInt();
            switch (i){
                case 1:
                    allMulu(course);
                    break;
                case 2:
                    selectMuluById(course);
                    break;
                case 3:
                    downLoad(course);
                    break;
                case 0:
                    return;
                default:
                    System.out.println("输入错误,请重新输入");


            }
        }
    }

    private void downLoad(Course course) {
        Integer a=null;
        while (true){
            System.out.println("是否继续进行下载?(Y/N)");
            String next1 = scanner.next();
            if (next1.equals("Y")){

            }else {
                break;
            }
            Result byCourseId = courseUnitService.findByCourseId(course.getId());
            if (byCourseId.getCode()==200){
                List<CourseUnit> obj = (List<CourseUnit>) byCourseId.getObj();
                for (CourseUnit courseUnit : obj) {
                    System.out.println("单元id"+courseUnit.getId()+"    单元名称:"+courseUnit.getName()+"    单元描述:"+courseUnit.getDecribe());
                }
                System.out.println("请输入想要下载的单元内容");
                int i = scanner.nextInt();
                Result byCourseUnitId = courseContentService.findByCourseUnitId(i);
                if (byCourseUnitId.getCode()==200){
                    List<CourseContent> obj1 = (List<CourseContent>) byCourseUnitId.getObj();
                    for (CourseContent courseContent : obj1) {
                        System.out.println("id:"+courseContent.getId());
                        System.out.println("名称:"+courseContent.getName());
                        System.out.println("目录:"+courseContent.getResourceSrc());
                        System.out.println("UnitId:"+courseContent.getUnitId());
                        System.out.println("~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~");
                    }
                    System.out.println("请输入想要下载的内容id");
                    int i1 = scanner.nextInt();
                        Result result = courseContentService.getCourseContentFile(i1);
                        if (result.getCode()==200){
                           a=i1;
                        }

                }
            }else {
                System.out.println("错误~~~~~~~~~~~~~~~~~~~~~~");

            }

            if (a!=null){
                Result result = courseContentService.getCourseContentFile(a);
                CourseFile courseFile = (CourseFile) result.getObj();
                byte[] fileContent = courseFile.getFileContent();
                String fileName = courseFile.getFileName();

                FileOutputStream fileOutputStream = null;
                try {
                    System.out.println("请输入想要下载到的目录位置");
                    String next = scanner.next();

                    fileOutputStream = new FileOutputStream(next+"\\" + fileName);
                    fileOutputStream.write(fileContent);
                    fileOutputStream.close();

                } catch (IOException e) {
                    e.printStackTrace();
                } finally {
                    System.out.println(result.getMsg());

                }
            }


        }
    }

    private void selectMuluById(Course course) {
        while (true){
            Result byCourseId = courseUnitService.findByCourseId(course.getId());
            if (byCourseId.getCode()==200){
                List<CourseUnit> obj = (List<CourseUnit>) byCourseId.getObj();
                for (CourseUnit courseUnit : obj) {

                    System.out.println("单元id"+courseUnit.getId()+"    单元名称:"+courseUnit.getName()+"    单元描述:"+courseUnit.getDecribe());

                }
                System.out.println("请输入想要查看的单元内容");
                int i = scanner.nextInt();
                Result byCourseUnitId = courseContentService.findByCourseUnitId(i);
                if (byCourseUnitId.getCode()==200){
                    List<CourseContent> obj1 = (List<CourseContent>) byCourseUnitId.getObj();
                    for (CourseContent courseContent : obj1) {
                        System.out.println("id:"+courseContent.getId());
                        System.out.println("名称:"+courseContent.getName());
                        System.out.println("目录:"+courseContent.getResourceSrc());
                        System.out.println("UnitId:"+courseContent.getUnitId());
                        System.out.println("~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~");
                    }
                    System.out.println("请输入想要查看的内容");
                    int i1 = scanner.nextInt();
                    Result result = courseContentService.getCourseContentFile(i1);
                    if (result.getCode()==200){
                        CourseFile obj2 = (CourseFile) result.getObj();
                        System.out.println("  名字:   "+obj2.getFileName());
                        System.out.println("内容:   "+new String(obj2.getFileContent()));
                    }
                }
            }else {
                System.out.println("错误~~~~~~~~~~~~~~~~~~~~~~");

            }
            break;
        }
    }

    private void allMulu(Course course) {
        while (true){
            Result byCourseId = courseUnitService.findByCourseId(course.getId());
            if (byCourseId.getCode()==200){
                List<CourseUnit> obj = (List<CourseUnit>) byCourseId.getObj();
                for (CourseUnit courseUnit : obj) {
                    System.out.println("单元id"+courseUnit.getId()+"    单元名称:"+courseUnit.getName()+"    单元描述:"+courseUnit.getDecribe());
                }
            }else {
                System.out.println("错误~~~~~~~~~~~~~~~~~~~~~~");
            }
            break;
        }
    }
}
